﻿using System;


/*使用事件机制，模拟实现一个闹钟功能。
 * 
 * 闹钟有 Tick 事件和 Alarm 两个事件。
 * 
 * 在闹钟走时时或者响铃时，在控制台显示提示信息。*/

namespace Clock
{
    class Program
    {
        static void Main(string[] args)
        {
            ThisClock thisClock = new ThisClock(10, 13);
            thisClock.thisClock.SClock(10, 15);
            thisClock.thisClock.ClockTick();
        }
    }
    //声明一个委托
    public delegate void Handler(object sender, Time args);
    
    public class Time
    {
        public int Hour { get; set; }
        public int Minute { get; set; }
        public int Second { get; set; }
    }
    /*public class ClockRing
    { 
        public void  Func(object sender,Time args)
        {
            Console.WriteLine("Ring Ring Ring！");
        }

    }
    */
    public class Clock
    {
        public event Handler Tick;
        public event Handler Alarm;

        private int hour, minute, second;
        int SHour, SMinute;

        //构造函数
        public Clock(int h, int m)
        {
            if (h < 24 && m < 60)
            {
                this.hour = h;
                this.minute = m;
            }
        }
        //设定闹钟启动时间
        public void SClock(int h, int m)
        {
            SHour = h;
            SMinute = m;


        }

        //时钟走时
        public void ClockTick()
        {
            while (true)
            {
                if (this.second < 59)
                    this.second++;
                else
                {
                    this.second = 0;
                    this.minute++;
                    if (minute == 60)
                    {
                        this.minute = 0;
                        this.hour++;
                    }
                }

                Time CurrentTime = new Time()
                {
                    Hour = hour,
                    Minute = minute,
                    Second = second
                };


                if (hour == SHour && minute == SMinute)
                {
                    Alarm(this, CurrentTime);
                }
                else
                {
                    Tick(this, CurrentTime);
                }
                System.Threading.Thread.Sleep(10000);
            }
        }
    }
    public class ThisClock
    {
        public Clock thisClock;
        public ThisClock(int hour,int minute)
        {
            thisClock = new Clock(hour, minute);
            thisClock.Tick += ThisClock_Tick;
            thisClock.Alarm += ThisClock_Alarm;
        }

        private void ThisClock_Alarm(object sender, Time args)
        {
            Console.WriteLine("Ring!");
        }

        private void ThisClock_Tick(object sender, Time args)
        {
           Console.WriteLine("CurrentTime:" + args.Hour + ":" + args.Minute + ":" + args.Second);
        }
    }
}
